{* Smarty *}
<script type="text/javascript" language="Javascript1.2">
{php}
sajax_show_javascript();
{/php}
{literal}
// --- [ View discounts ] ---------------------
function do_items_cb(ss)
{
    var f = document.forms["edit_type"];
    var e = f.elements["items_list[]"];
    e.options.length=0;

    if("" != ss)
    {
        var z = "asd"; z = ss;
        z = z.split("~~");
        idx = 0;
        for (v in z)
        {
            vls = z[v].split("|");
            e.options[idx++] = new Option(vls[1], vls[0]);
        }
    } // end if
}

// --- [ Select type discount ] ---------------------

function do_list_items(n_f)
{
    num_form = n_f;
    s = "";
    var f = document.forms["edit_type"];
    var e = f.elements["catalog"];

    for(i=0; i<e.options.length; i++)
    {
        if(e.options[i].selected)
        {
            s += (s=="" ? "" : "|")+e.options[i].value;
        }
    }
    x_GetListDiscounts(s, do_items_cb);
}


</script>


<script language="javascript">
function AddItem(n_f)
{
    var f = document.forms["edit_type"];
    if(!f) 
    {
        alert("Unknown error!");
        return false;
    }
    
    var e1 = f.elements['items_list[]']; 
    var e2 = f.elements['selected_items_list[]'];
    for (i = 0; i < e1.length; i++)
    {
        if (e1[i].selected)
        {
            flag = true;
            for (j = 0; j < e2.length; j++)
                if (e2[j].value == "d" + e1[i].value)
                {
                    flag = false;
                    break;
                }

            if (flag)
                e2.options[e2.length] = new Option(e1[i].text, "d" + e1[i].value);
        }
    }
}



function AddCatalog(n_f)
{
    var f = document.forms["edit_type"];
    if(!f) 
    {
        alert("Unknown error!");
        return false;
    }
    
    var e1 =  f.elements['catalog'].options[f.elements['catalog'].selectedIndex];
    var e2 = f.elements['selected_items_list[]'];
    
    
    if (0 != e1.value)
    {
        flag = true;
        for (j = 0; j < e2.length; j++)
            if (e2[j].value == ("t" + e1.value))
            {
                flag = false;
                break;
            }

        if (flag)
            e2.options[e2.length] = new Option("t" + e1.text, e1.value);
    }
}



function DellItem(n_f)
{
    var f = document.forms["edit_type"];
    if(!f) 
    {
        alert("Unknown error!");
        return false;
    }
    
    var e = f.elements['selected_items_list[]'];
    for (i = e.length - 1; i >= 0; i--)
    {
        if (e[i].selected)
        {
             e.options[i] = null;
        }
    }
}
</script>
<script language="javascript">
function CheckData()
{
    var f = document.forms["edit_type"];
    if(!f) 
    {
        alert("Unknown error!");
        return false;
    }

    if ("" == f.elements['title'].value)
    {
        alert("Type Discount Title is empty.");
        return false;
    }

    for (i = 0; i < f.elements['selected_items_list[]'].length; i++)
    {
        f.elements['selected_items_list[]'].options[i].selected = true;
    }
    
    return true;
}
</script>
{/literal}

<form action="{$action_url}" method="post" name="edit_type"  onSubmit="javascript : return CheckData();">
<input type="hidden" name="id" value="{$id}" />
{if 1 == $edit}
<input type="hidden" name="event" value="doSubmitEditDiscountType" />
{else}
<input type="hidden" name="event" value="doSubmitAddDiscountType" />
{/if}
<table>
<tr>
    <td>
        Title Discount
    </td>
    <td>
        <input type="text" name="title" value="{$title}" />
    </td>
</tr>

<tr>
      <td align="right">Types disconts and discounts</td>
      <td>
      
      <table>
      <tr>
      <td colspan=3>

        <select name="catalog" onChange="javascript: do_list_items();">
        <option value="0">None</option>
        {foreach from = $types_discounts item = type}
            <option value="{$type.id_type}">{$type.title}</option>
        {/foreach}
        </select>
        &nbsp;&nbsp;&nbsp;
        <input class="button" type="button" value=">>" align="left" onClick="javascript : AddCatalog();" />
      </td>
      </tr>
      <tr>

      <td>
<!--Select Items-->
        <div align="left">Target Discounts</div>
        <table align="left" >
        <tr>
            <td>
            <div >
            <select name="items_list[]" size=15 style="width:330px;" multiple>
            </select>
            </div>
            </td>
        </tr>
        </table>
      </td>

      <td>
          <input class="button" type="button" value=">>" onClick="javascript : AddItem();" />
          <br />
          <br />
          <br />
          <input class="button" type="button" value="<<" onClick="javascript : DellItem();" />
      </td>


      <td>
<!--Selected Items-->
        <div align="left">Selected Discounts</div>
        <table width="100%" align="left" >
        <tr>
            <td>
            <select name='selected_items_list[]' size=15 style="width:330px;" multiple>
            {foreach from = $arr_types_selected item = type}
                <option value="t{$type.id}">{$type.title}</option>
            {/foreach}
            {foreach from = $arr_discounts_selected item = disc}
                <option value="d{$disc.id}">{$disc.title}</option>
            {/foreach}
            </select>
            </td>
        </tr>
        </table>
      </td>
      </tr>
      </table>
        
      </td>
</tr>
</table>

<br />
<input type="submit" value="Submit" />

</form>
